/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.auth.DisAppAuthenticator;
import com.sigmatrix.h5.auth.ILoginResponse;
import com.sigmatrix.h5.dto.LoginData;
import com.sigmatrix.h5.enums.ResponseEnum;
import com.sigmatrix.h5.enums.SysResModule;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.exception.ServiceException;
import com.sigmatrix.h5.response.Response;
import com.sigmatrix.h5.service.DisDealerAcctService;
import com.sigmatrix.h5.utils.Cons;
import com.sigmatrix.h5.web.args.LoginParam;
import com.sigmatrix.h5.web.controller.common.BaseController;
import com.sigmatrix.h5.web.vo.LoginVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName: LoginController
 * @Description: 登录接口
 * @date 2017年8月15日 上午10:59:37
 */
@Api(tags = "登录接口")
@RestController
@RequestMapping("/app/crm")
public class LoginController extends BaseController {

	/** logger record */
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

	/** 用户账号业务层接口 */
	@Autowired
	private DisDealerAcctService disDealersAcctService;

	@Autowired
	private DisAppAuthenticator authenticator;

	/**
	 * @Title: login
	 * @Description: 登录接口
	 * @return Response
	 * @date 2017年8月15日 上午11:08:47
	 * @param request
	 * @param loginVo
	 * @param result
	 */
	// @ApiOperation(value="登录接口")
	// @PostMapping(value="/login")
	// public Response login(@Valid @RequestBody final LoginVo loginVo,final
	// BindingResult result) {
	// LOGGER.info("登录接口请求：{}",JSONObject.toJSONString(loginVo));
	// try {
	// if (result.hasErrors()) {
	// final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
	// LOGGER.error("登录请求参数验证失败{}", errInfo);
	// return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
	// }
	// final Response response = disDealersAcctService.validateLogin(loginVo);
	// LOGGER.info("登录接口返回：{}",JSONObject.toJSONString(response));
	// return response;
	// } catch (ServiceException e) {
	// LOGGER.error("登录请求接口异常", e);
	// return new Response(ResponseEnum.EXCEPTION);
	// }
	// }

	//
	@ApiOperation(value = "登录接口2")
	@PostMapping(value = "/login")
	public Response loginV2(@Valid @RequestBody final LoginVo loginVo, final BindingResult result) {
		LOGGER.info("登录接口请求：{}", JSONObject.toJSONString(loginVo));
		try {
			if (result.hasErrors()) {
				final String errInfo = result.getAllErrors().get(0).getDefaultMessage();
				LOGGER.error("登录请求参数验证失败{}", errInfo);
				return new Response(Cons.RESPONSE_CODE_PARAM_INVALID, errInfo);
			}
			LoginParam param = new LoginParam();
			param.setLoginName(loginVo.getAccount());
			param.setLoginPwd(loginVo.getPwd());
			param.setLoginType(SysResModule.DISTRIBUTION_APP.code);
			Response response;
			try {
				LoginData loginData = (LoginData) authenticator.login(param);
				response = new Response(ResponseEnum.SUCCESS, loginData);
			} catch (MessageException e) {
				LOGGER.error("登录异常", e);
				response = new Response(null == e.getStatus() ? 409 : Integer.parseInt(e.getStatus()), e.getMessage());
			}
			LOGGER.info("登录接口返回：{}", JSONObject.toJSONString(response));
			return response;
		} catch (Exception e) {
			LOGGER.error("登录请求接口异常", e);
			return new Response(ResponseEnum.EXCEPTION);
		}
	}
}
